# -*- coding: utf8 -*
"""
Test suite for content types ActivityReport*.
"""



from Testing import ZopeTestCase
from Products.PloneTestCase import PloneTestCase
from AccessControl import Unauthorized
from Products.CMFPlone.utils import safe_hasattr, base_hasattr
from base import ContentTypesTestCase
from Products.ContentTypes import config


class TestActivityReport(ContentTypesTestCase):
    """
    Test suite for ActivityReport
    """

    def afterSetUp(self):
        self.createUsers()
        self.createContent()

    def createContent(self):
        """ Create content needed to run tests.
        """
        self.folder.invokeFactory(type_name='ActivityReportFolder',
            id='reports', title='Reports')
        self.folder.reports.invokeFactory(type_name='PActivityReport',
            id='report1')

        # the post_scripts only run during processForm, so we need to call
        # it explicitly
        self.folder.reports.report1.at_post_create_script()


    def createUsers(self):
        # create users and assign them to group 'dme'
        ms_tool = self.portal.portal_membership
        acl_users = self.portal.acl_users
        gd_tool = self.portal.portal_groups

        GROUPS = (
            ('gep', {'title': 'Gabinete de Estudos e Planeamento'}, (), ()),
            ('dme', {'title': 'Departamento Municipal de Estudos'}, (), ()),
            ('sf', {'title': 'Sector de Financiamentos'}, (), ()),  
        )

        for groupname, properties, roles, groups in GROUPS:
            if not gd_tool.getGroupById(groupname):
                acl_users.userFolderAddGroup(groupname, roles, groups)
                # set properties
                group = gd_tool.getGroupById(groupname)
                group.setProperties(properties=properties)


        USERS = (
            ('user1', '123456', {'fullname': 'User Test', 
                'email': 'user@nospam.com'}, [], ('dme',)),
            ('user2', '123456', {'fullname': 'User Test 2', 
                'email': 'user2@nospam.com'}, [], ('dme',)),
        )

        for username, password, properties, roles, groups in USERS:
            if not ms_tool.getMemberById(username):
                acl_users.userFolderAddUser(username, password, roles,
                    [], groups=groups)
                # set properties
                member = ms_tool.getMemberById(username)
                member.setMemberProperties(properties)


    def testTitle(self):
        report = self.folder.reports.report1
        report.setMonth('01')
        report.setYear('2009')
        report.setUsername('user1')
        self.failUnlessEqual(report.Title(), '01/2009 - User Test')


def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(TestActivityReport))
    return suite


import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))
